package com.second.controller.coupon;

import com.second.model.dto.LAndR.LAndRResult;
import com.second.model.pojo.FCoupon;
import com.second.service.IFCouponService;
import com.second.service.IFCouponUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@Controller
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    private IFCouponUserService couponUserService;

    @Autowired
    private IFCouponService couponService;


    @GetMapping("")
    public String test01() {
        return "jsp/coupon/coupon";
    }


    //全部待使用
    @PostMapping("/{userId}")
    @ResponseBody
    public LAndRResult getAll(@PathVariable("userId") Integer userId) {
        System.out.println(userId);
        List<FCoupon> list = couponUserService.findAllusercoupon(userId);

        return new LAndRResult("200","查询成功",list);
    }
    //领取
    @GetMapping("/receive/{userId}/{couponId}")
    @ResponseBody
    public void receiveCoupon(@PathVariable("userId") Integer userId, @PathVariable("couponId") Integer couponId,
                              HttpServletResponse response) throws IOException {
        couponUserService.receiveCoupon(couponId, userId);
        response.sendRedirect("http://localhost:8081/fr/coupon");

    }

    //优惠券过期
    @PostMapping("/outdate/{userId}")
    @ResponseBody
    public List<FCoupon> getUserCoupon(@PathVariable("userId") Integer userId) {
        return couponUserService.usedByuserId(userId);
    }


    //所有优惠券-点赞数
    @PostMapping("/findAllZan/{userId}")
    @ResponseBody
    public List<FCoupon> findAllCouponZan(@PathVariable("userId") Integer userId) {
        List<FCoupon> fCoupons = couponService.findAllZan(userId);

        return fCoupons;
    }
    //所有优惠券-评论数
    @PostMapping("/findAllComment/{userId}")
    @ResponseBody
    public List<FCoupon> findAllCoupon(@PathVariable("userId") Integer userId) {

        return couponService.findAllComment(userId);
    }


    //使用
//    @GetMapping("/used/{Id}")
//    @ResponseBody
//    public LAndRResult updateById(@PathVariable("Id") Integer Id) {
//        return couponUserService.updateById(Id);
//    }

    //展示使用
    @PostMapping("/couponused/{userId}")
    @ResponseBody
    public List<FCoupon> usingCoupon(@PathVariable("userId") Integer userId) {
        return couponUserService.usingCoupon(userId);
    }

}
